package com.hawk.eye.server.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hawk.eye.framework.utils.CollectionUtil;
import com.hawk.eye.server.constant.GroupConstant;
import com.hawk.eye.server.convert.GroupConvert;
import com.hawk.eye.server.domain.dto.req.GroupReqDTO;
import com.hawk.eye.server.domain.dto.resp.GroupRespDTO;
import com.hawk.eye.server.domain.dto.resp.TreeRespDTO;
import com.hawk.eye.server.domain.po.Group;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hawk.eye.server.mapper.GroupMapper;
import com.hawk.eye.server.service.IGroupService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Service业务层处理
 *
 * @author salad
 * @date 2023-09-23
 */
@Service
@RequiredArgsConstructor
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements IGroupService {

    private final GroupMapper groupMapper;

    @Override
    public List<TreeRespDTO> list(GroupReqDTO reqDTO) {
        List<TreeRespDTO> tree = new ArrayList<>();
        tree.add(new TreeRespDTO(GroupConstant.DEFAULT_GROUP.getValue(), GroupConstant.DEFAULT_GROUP.getName(),"group"));
        List<Group> groups = groupMapper.selectGroupList(reqDTO.getGroupName());
        if (CollectionUtil.isEmpty(groups)) {
            return tree;
        }
//        List<Long> groupIds = StreamUtil.mapToList(groups.stream(), Group::getId);
//        List<ServiceConfig> services = serviceConfigMapper.selectServiceByGroupIds(groupIds);
//        Map<Long, List<ServiceConfig>> serviceConfigGrouped = StreamUtil.groupBy(services.stream(), ServiceConfig::getGroupId);
        for (Group group : groups) {
            TreeRespDTO treeRespDTO = new TreeRespDTO();
            treeRespDTO.setId(group.getId());
            treeRespDTO.setName(group.getGroupName());
            treeRespDTO.setType("group");
//            List<ServiceConfig> serviceConfigs = serviceConfigGrouped.get(group.getId());
//            if (CollectionUtil.isNotEmpty(serviceConfigs)) {
//                List<TreeRespDTO> childes = new ArrayList<>();
//                for (ServiceConfig serviceConfig : serviceConfigs) {
//                    TreeRespDTO children = new TreeRespDTO();
//                    children.setId(serviceConfig.getId());
//                    children.setType("service");
//                    children.setName(StringUtil.format("{}({})", serviceConfig.getServiceCnName(), serviceConfig.getServiceZhName()));
//                    childes.add(children);
//                }
//                treeRespDTO.setChildren(childes);
//            }
            tree.add(treeRespDTO);
        }
        return tree;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(GroupReqDTO reqDTO) {
        return groupMapper.insert(GroupConvert.INSTANCE.reqDtoToPo(reqDTO)) > 0;
    }

    @Override
    public GroupRespDTO detail(Long id) {
        Group group = groupMapper.selectById(id);
        return GroupConvert.INSTANCE.poToRespDto(group);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(GroupReqDTO reqDTO) {
        return groupMapper.update(GroupConvert.INSTANCE.reqDtoToPo(reqDTO),
                Wrappers.<Group>lambdaUpdate().eq(Group::getId, reqDTO.getId())
                        .set(Group::getGroupName, reqDTO.getGroupName())) > 0;
    }

}
